package com.happy.xiao.common.base;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.CompletionException;

/**
 * 这里是对异常捕捉
 */
@ControllerAdvice
@RestController
public class AppControllerActive {

    private static final Logger logger = LoggerFactory.getLogger(AppControllerActive.class);

    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public BaseResponseInfo<String> errorHandler(Exception s) {
        logger.error(s.getMessage());
        System.out.println(s.getMessage());
        System.out.println(s);

        if (s instanceof CompletionException) {

            if (s.getCause() instanceof MyErrorException) {
                MyErrorException ex = (MyErrorException) s.getCause();
                int code = ex.getCode();
                String message = ex.getMessage();
                return new BaseResponseInfo<>(code, message);
            }

        } else if (s instanceof MyErrorException) {
            MyErrorException ex = (MyErrorException) s;
            int code = ex.getCode();
            String message = ex.getMessage();
            return new BaseResponseInfo<>(code, message);
        }
        return new BaseResponseInfo<>(-1, "服务器异常");
    }

    @RequestMapping(name = "/error")
    public BaseResponseInfo<String> error404() {
        return new BaseResponseInfo<>(-1, "该接口不存在");
    }


}
